package com.lhy.common.web.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.enums.SqlKeyword;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lhy.common.web.entity.BasePage;
import com.lhy.common.web.entity.ReqCriteria;
import com.lhy.common.web.entity.ReqPage;
import com.lhy.common.web.entity.RespPage;
import com.lhy.common.web.wrapper.WrapperHelper;

import java.util.List;
import java.util.Optional;

/**
 * 简易增删改查服务，在mybatis-plus service基础上扩展了一些常用方法
 *
 * @param <M>
 * @param <T>
 */
public abstract class SimpleService<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements ExtendService<T> {

    @Override
    public Optional<T> getOne(T bean) {
        return this.list(WrapperHelper.newAndEqQueryWrapper(bean, getEntityClass())).stream().findFirst();
    }

    @Override
    public List<T> list(T bean) {
        return this.list(WrapperHelper.newAndEqQueryWrapper(bean, getEntityClass()));
    }

    @Override
    public boolean remove(T bean) {
        return this.remove(WrapperHelper.newAndEqQueryWrapper(bean, getEntityClass()));
    }

    @Override
    public boolean exist(T bean) {
        return !this.list(WrapperHelper.newAndEqQueryWrapper(bean, getEntityClass())).isEmpty();
    }

    @Override
    public RespPage<T> paging(ReqPage simplePage, T bean) {
        QueryWrapper<T> queryWrapper = WrapperHelper.newAndLikeQueryWrapper(bean, getEntityClass());
        simplePage.getSort().forEach(sort -> queryWrapper.orderBy(true, SqlKeyword.ASC.getSqlSegment().toLowerCase().equals(sort.getOrder()), sort.getName()));
        Page<T> page1 = toPlusPage(simplePage);
        IPage<T> page = this.page(page1, queryWrapper);
        return toRespPage(simplePage, page);
    }

    /**
     * toPlusPage
     * <p/>
     * 分页page转为PlusPage
     *
     * @param simplePage
     * @return com.baomidou.mybatisplus.extension.plugins.pagination.Page
     * @throws
     * @date 2021/5/18 17:27
     */
    protected Page<T> toPlusPage(BasePage simplePage) {
        return new Page<>(simplePage.getPage().longValue(), simplePage.getRows().longValue());
    }


    /**
     * toPage
     * <p/>
     * 分页page转为PlusPage
     *
     * @param pageReq
     * @param iPage
     * @return Page
     * @throws
     * @date 2021/5/18 17:27
     */
    protected RespPage<T> toRespPage(BasePage pageReq, IPage<T> iPage) {
        return new RespPage<>(pageReq.getPage(), pageReq.getRows(), (int) iPage.getTotal(), iPage.getRecords());
    }

    /**
     * updateSelectiveById
     * <p/>
     * 根据主键更新非空字段
     *
     * @param bean
     * @return boolean
     * @throws
     * @date 2019/3/18 17:27
     */
    @Override
    public boolean updateSelectiveById(T bean) {
        UpdateWrapper<T> updateWrapper = new UpdateWrapper<>();
        WrapperHelper.setWrapperWhere(bean, updateWrapper);
        WrapperHelper.setWrapperSet(bean, updateWrapper);
        return this.update(null, updateWrapper);
    }

    public RespPage<T> searchCriteria(ReqCriteria criteriaReq) {
        Class<T> entityClass = getEntityClass();
        QueryWrapper<T> queryWrapper = WrapperHelper.newConditionWrapper(criteriaReq.getSc(), entityClass);
        Page<T> page = this.page(toPlusPage(criteriaReq), queryWrapper);
        return toRespPage(criteriaReq, page);

    }


}
